package com.sbio.udiapp.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sbio.udiapp.R;
import com.sbio.udiapp.listener.ListAdapterListener;
import com.sbio.udiapp.util.StringUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OutStockApplyAdapter extends RecyclerView.Adapter<OutStockApplyAdapter.MyViewHolder> {


    private List<String> nos;
    private Map<String, String> bpNoStocks;

    private Map<String,Integer> no2Qty;


    private ListAdapterListener listener;

    // 构造方法，接受数据集合作为参数
    public OutStockApplyAdapter(List<String> dataList, ListAdapterListener listener) {
        this.nos = dataList;
        this.listener = listener;
        this.bpNoStocks = new HashMap<>();
        this.no2Qty = new HashMap<>();
    }

    // 构造方法，接受数据集合作为参数
    public OutStockApplyAdapter(List<String> dataList, ListAdapterListener listener, Map<String, String> bpNoStocks, Map<String, Integer> no2Qty) {
        this.nos = dataList;
        this.listener = listener;
        this.bpNoStocks = bpNoStocks;
        this.no2Qty = no2Qty;
    }

    // 创建 ViewHolder，将列表项的布局传递给构造函数
    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_out_stock_apply_item, parent, false);
        return new MyViewHolder(view);
    }

    // 绑定数据到 ViewHolder
    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        String data = nos.get(position);
        String data2 = bpNoStocks.get(data);
        Integer qty = no2Qty.get(data);
        holder.bind(data, data2,qty);
    }

    // 返回数据集合的大小
    @Override
    public int getItemCount() {
        return nos.size();
    }

    // ViewHolder 类
    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv;
        Button remove;

        String key;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tv = itemView.findViewById(R.id.tv_box_code);
            remove = itemView.findViewById(R.id.remove);
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            listener.onItemClick(position);
                        }
                    }
                }
            });
        }

        public void bind(String data, String data2,Integer qty) {
            key = data;
            String temp = StringUtil.prettyUdi(data,data2,qty);
            tv.setText(temp);
            remove.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.remove(key);
//                    dataList.remove();
//                    notifyDataSetChanged();
                }
            });
        }

    }


}
